package com.zwg.tingshu.order.delay;
import com.alibaba.fastjson.JSON;
import com.zwg.tingshu.common.constant.RocketMqConstant;
import com.zwg.tingshu.common.service.RocketMqService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Component
public class DelayCloseOrderService {

    @Autowired
    private RocketMqService rocketMqService;

    /**
     * 基于rocketmq实现5分钟自动关单（按照10/10/20s轮询处理） 首尾30s策略
     *
     * @param data 数据
     */
    public void sendDelayMessage(String data) {
        try {
            //1. 创建延迟消息
            List<Long> times = new ArrayList<>();
            times.add(30L);
            for(int i=0;i<6;i++) {
                times.add(10L);
                times.add(10L);
                times.add(20L);
            }
            times.add(30L);
            DelayCloseMessage message = DelayCloseMessage.builder().data(data).times(times).build();
            //2. 发送延迟消息
            rocketMqService.sendAsync(RocketMqConstant.QUEUE_ORDER_CANCEL, RocketMqConstant.QUEUE_ORDER_CANCEL, JSON.toJSONString(message));
            log.info("发送延迟消息成功：{}", data);
        } catch (Exception e) {
            log.error("[延迟消息]发送异常：{}", data);
            throw new RuntimeException(e);
        }
    }
}